using System;
using System.Collections.Generic;
using System.Text;

using Peradigm.Framework.Domain.Security;
using Peradigm.Framework.Domain.Security.Exceptions;
using Peradigm.Framework.Domain.Exceptions;
using Peradigm.Framework.Security;

using Liberty.Application.Security.Exceptions;
using Liberty.Persistence.Security;

namespace Liberty.Application.Security
{
    [Serializable]
    public class SecurityController : AbstractController
    {
        #region Constants
        private const string 
            LOGIN_FAILED_MESSAGE_FORMAT = "Login failed - {0}",
            LOGIN_FAILED_MESSAGE = "Login failed. Invalid User";
        #endregion

        #region Event Handlers
        #endregion

        #region Methods
        public User AuthenticateUserWith( string userName, string password )
        {
            try
            {
                UserBroker userBroker = new UserBroker(this.ConnectionString);

                User user = this.SecurityService.UserWith(userName, password);
                if( user != null )
                {
                    user = userBroker.SaveUser( user );
                }

                userBroker.SaveUserRoles( user );

                return user;
            }
            catch( SecurityException e )
            {
                throw new LoginFailedException(
                    String.Format( LOGIN_FAILED_MESSAGE_FORMAT, e.Message ), e, Severity.High );
            }
            catch( Exception e )
            {
                throw new LoginFailedException(
                    String.Format( LOGIN_FAILED_MESSAGE_FORMAT, e.Message ), e, Severity.High );
            }
            throw new LoginFailedException(
                    String.Format( LOGIN_FAILED_MESSAGE ), Severity.High );
        }
        #endregion

        #region Properties
        #endregion

        #region Private Methods
        #endregion

        #region Private Properties
        private SecurityService SecurityService
        {
            get
            {
                return i_SecurityService;
            }
            set
            {
                i_SecurityService = value;
            }
        }
        #endregion

        #region Construction and Finalization
        static SecurityController()
        {
            string buildVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
            string serverName = System.Environment.MachineName;

            SecurityService.ApplicationService = new LibertyApplication();
            Peradigm.Framework.Domain.Security.Application application = SecurityService.ApplicationService;
            application.BuildVersion = buildVersion;
            application.ServerName = serverName;
        }

        public SecurityController()
            : base()
        {
            this.SecurityService = new SecurityService();
        }
        #endregion

        #region Data Elements
        private SecurityService i_SecurityService;
        #endregion

    }
}
